package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.bean.TvActorBean;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityGame1Binding;
import flc.ast.utils.Content;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.ToastUtil;
import stark.common.basic.view.container.StkRelativeLayout;

public class Game1Activity extends BaseAc<ActivityGame1Binding> {

    //看图猜角色
    public static int level = 0;
    private List<TvActorBean> listGame;
    private List<String> listAnswer = new ArrayList<>();
    private int rightNum;


    @Override
    protected int onCreate() {
        return R.layout.activity_game1;
    }

    @Override
    protected void initView() {
        mDataBinding.ivGame1Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvGame1OptionA.setOnClickListener(this);
        mDataBinding.tvGame1OptionB.setOnClickListener(this);
        mDataBinding.tvGame1OptionC.setOnClickListener(this);
        mDataBinding.tvGame1OptionD.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()){
            case R.id.tvGame1OptionA://选项A
                isRight(1, mDataBinding.ivGame1OptionAImg);
                break;
            case R.id.tvGame1OptionB://选项B
                isRight(2, mDataBinding.ivGame1OptionBImg);
                break;
            case R.id.tvGame1OptionC://选项C
                isRight(3, mDataBinding.ivGame1OptionCImg);
                break;
            case R.id.tvGame1OptionD://选项D
                isRight(4, mDataBinding.ivGame1OptionDImg);
                break;
        }
    }

    @Override
    protected void initData() {
        listGame = GtDataProvider.getActorData();
        if (listGame == null || listGame.size() == 0) return;
        for (TvActorBean bean : listGame) {
            listAnswer.add(bean.name);
        }
        setData(listGame.get(level));
    }

    //设置游戏数据
    private void setData(TvActorBean bean) {
        mDataBinding.tvGame1Title.setText(getString(R.string.level) + (level + 1));
        mDataBinding.ivGame1Img.setImageBitmap(GtDataProvider.getGuessImgBitmap(bean.imgName));//图片
        mDataBinding.ivTipImg.setVisibility(View.INVISIBLE);
        List<String> list = RandomUtil.randomGetItems(listAnswer, bean.name, 3);
        mDataBinding.tvGame1OptionA.setText(list.get(0));
        mDataBinding.tvGame1OptionB.setText(list.get(1));
        mDataBinding.tvGame1OptionC.setText(list.get(2));
        mDataBinding.tvGame1OptionD.setText(list.get(3));
        boolean isA = checkAnswer(list.get(0), bean.name);
        boolean isB = checkAnswer(list.get(1), bean.name);
        boolean isC = checkAnswer(list.get(2), bean.name);
        boolean isD = checkAnswer(list.get(3), bean.name);
        if (isA) {
            rightNum = 1;
            mDataBinding.ivGame1OptionAImg.setImageResource(R.drawable.shape_option_right);
        } else {
            mDataBinding.ivGame1OptionAImg.setImageResource(R.drawable.shape_option_bg);
        }
        if (isB) {
            rightNum = 2;
            mDataBinding.ivGame1OptionBImg.setImageResource(R.drawable.shape_option_right);
        } else {
            mDataBinding.ivGame1OptionBImg.setImageResource(R.drawable.shape_option_bg);
        }
        if (isC) {
            rightNum = 3;
            mDataBinding.ivGame1OptionCImg.setImageResource(R.drawable.shape_option_right);
        } else {
            mDataBinding.ivGame1OptionCImg.setImageResource(R.drawable.shape_option_bg);
        }
        if (isD) {
            rightNum = 4;
            mDataBinding.ivGame1OptionDImg.setImageResource(R.drawable.shape_option_right);
        } else {
            mDataBinding.ivGame1OptionDImg.setImageResource(R.drawable.shape_option_bg);
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }

    //判断对错
    private void isRight(int kind, ImageView viewLog) {
        viewLog.setVisibility(View.VISIBLE);
        if (kind == rightNum) {
            mDataBinding.ivTipImg.setImageResource(R.drawable.dui1);
            mDataBinding.ivTipImg.setVisibility(View.VISIBLE);
            ToastUtils.showShort(getString(R.string.answer_right));
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //判断是否需要解锁下一关
                    if (level < (listGame.size() - 1)) {//如果当前关卡小于总关卡数
                        int spLevel = SPUtil.getInt(mContext, Content.GAME_NAME1, 1);//获取记录通过关卡数
                        if (level == (spLevel - 1)) {//如果通过的是未解锁关卡则解锁下一关
                            SPUtil.putInt(mContext, Content.GAME_NAME1, spLevel + 1);
                        }
                        if (level == (listGame.size() - 1)) {
                            ToastUtils.showShort(R.string.it_is_already_the_last_level);
                            setData(listGame.get(level));
                            return;
                        }
                        level++;
                        setData(listGame.get(level));
                    }
                    viewLog.setVisibility(View.INVISIBLE);
                }
            }, 1000);
        } else {
            mDataBinding.ivTipImg.setImageResource(R.drawable.cuo1);
            mDataBinding.ivTipImg.setVisibility(View.VISIBLE);
            ToastUtils.showShort(getString(R.string.answer_error));
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewLog.setVisibility(View.INVISIBLE);
                }
            }, 1000);
        }
    }

}